package netx

import (
	"net"
	"net/http"
)

func IsActiveTCPAddr(addr string) (bool, error) {
	tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		return false, err
	}
	defer conn.Close()

	return true, nil
}

//获取ip地址
func GetClientIPByHeaders(req *http.Request) (ip string) {

	// Client could be behid a Proxy, so Try Request Headers (X-Forwarder)
	ipSlice := []string{}

	ipSlice = append(ipSlice, req.Header.Get("X-Forwarded-For"))
	ipSlice = append(ipSlice, req.Header.Get("x-forwarded-for"))
	ipSlice = append(ipSlice, req.Header.Get("X-FORWARDED-FOR"))

	for _, v := range ipSlice {
		//log.Printf("debug: client request header check gives ip: %v", v)
		if v != "" {
			return v
		}
	}
	return ""

}
